package com.rain.example.verifyPermissions.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ResultUtil {
    // 响应JSON的方法
    public static void responseJson(HttpServletResponse response, String resultJSON) throws IOException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");
        response.setStatus(HttpStatus.OK.value()); // 或者根据result.code设置不同的HTTP状态码
        response.getWriter().write(resultJSON);
    }

    public static String resultCode(int code, String msg) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("msg", msg);
        return JSONObject.toJSONString(map);
    }
}
